﻿/*-----------------------------------------------
// File: BCAttackState.cs 
// Description: 渲染层，英雄对象攻击状态 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class BCAttackState : Fsm.FsmStateMonoBase<ActorState>
    {
        BattleCharacter bc {
            get { return this.controller as BattleCharacter; }
        }
        public BCAttackState(ActorState state, BattleCharacter ctrl) : base(state, ctrl) { }

        float deltaTime = 0f;
        BattleSkill currentSkill = null;
        Queue<BattleSkillRelease> releaseQueue = new Queue<BattleSkillRelease>();
        BattleSkillRelease currentRelease = null;

        public List<BattleSkillRelease> GetLastReleases() {
            List<BattleSkillRelease> res = new List<BattleSkillRelease>();
            foreach (var r in releaseQueue) {
                res.Add(r);
            }
            if (currentRelease != null) {
                res.Add(currentRelease);
            }
            return res;
        }
        public override void Enter()
        {
            deltaTime = 0f;
            var currentPackage = bc.GetCurrentPackage();
            currentSkill = bc.GetSkill(currentPackage.skillType);
            releaseQueue = new Queue<BattleSkillRelease>();
            bc.isHealer = true;
            foreach (var release in currentSkill.releases) {
                releaseQueue.Enqueue(release);
                if (release.exportType != (int)Engine.AttackType.TREAT)
                    bc.isHealer = false;
            }
            currentRelease = null;
            currentSkill.Do();
        }

        public override void Update()
        {
            deltaTime += Time.deltaTime;
            if (currentRelease == null) {
                if (releaseQueue.Count > 0) {
                    currentRelease = releaseQueue.Dequeue();
                }
            }
            if (currentRelease != null) {
                if (deltaTime >= currentRelease.releaseTime) {
                    OnSkillRelease(currentRelease);
                    currentRelease = null;
                }
            }

            if (deltaTime >= currentSkill.animDuration) {
                bc.SwitchState(ActorState.AttackWait);
            }
        }

        public override void Leave()
        {
            bc.DonePackage();
        }
        void OnSkillRelease(BattleSkillRelease release) {
            if (!release.haveProjectile)
            {
                //AttackRespect respect = new AttackRespect(release.HeroUid, release.SkillType, release.HSRId);
                //ViewRespectCtrl.GetInstance().AddRespect(respect, OnSkillReleaseCallBack);
            }
            else {
                ProjectileRespect respect = new ProjectileRespect(release.HeroUid, release.SkillType, release.HSRId);
                ViewRespectCtrl.GetInstance().AddRespect(respect, OnSkillReleaseShoot);
            }

            var targetBC = BattleViewCtrl.GetInstance().GetBattleCharacter(bc.GetCurrentPackage().FPIdCamp, bc.GetCurrentPackage().FPId);
            currentSkill.OnSkillRelease(release, targetBC);
        }
        void OnSkillReleaseShoot(ResponseBase rp) {
            var response = rp as ProjectileResponse;
            if (response == null) {
                Debug.LogError("[BCAttack OnSkillReleaseShoot] Response Not ProjectileResponse. TYPE:" + rp.GetType().ToString());
                return;
            }

            var _bc = BattleViewCtrl.GetInstance().GetBattleCharacter(response.sourceUid);
            var _skill = _bc.GetSkill(response.skillType);
            var _release = _skill.GetSkillRelease(response.releaseId);
            if (_release.haveProjectile)
            {
                BattleViewCtrl.GetInstance().ShootProjectile(new ProjectilePacket(_release.projData, _release.HeroUid, _release.SkillType, _release.HSRId, response.FPId, response.FPCamp));
            }
            else {
                Debug.LogError("[OnSkillReleaseShoot] Release Not have Projectile Data. releaseId:" + _release.HSRId);
            }
        }
        void OnSkillReleaseCallBack(ResponseBase rp) {
            var attackPackage = rp.packageData as OnceAttackPackage;
            if (attackPackage == null) {
                Debug.LogError("[BCAttackState OnSkillReleaseCallBack] Response Not Have OnceAttackPackage. TYPE:" + rp.packageData.GetType().ToString());
                return;
            }
            attackPackage.NormalDeal();


        }
    }

}